package com.chidopi.market.aa.service.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chidopi.cdf.dao.IBaseDao;
import com.chidopi.cdf.exception.CdfException;
import com.chidopi.cdf.service.IBaseService;
import com.chidopi.market.aa.domain.Accounts;
import com.chidopi.market.aa.domain.Roles;

public class AccountServiceImpl implements IBaseService<Accounts> {
	private static Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);
	@Autowired
	@Qualifier("accountDao")
	private IBaseDao<Accounts> baseDao;
	
	@Autowired
	@Qualifier("roleDao")
	private IBaseDao<Roles> roleDao;

	public int delete(Accounts t) throws Exception {
		Integer id = t.getId();
		List<Roles> roles = this.roleDao.find("id in (select role_id from account2role where account_id=?) ", id);
		if (roles.size() > 0) {
			logger.debug("delete [" + id + "] failure: account2role.size()=" + roles.size());
			throw new CdfException("error.account.delete.account2role");
		}
		return this.baseDao.delete(t);
	}

	public List<Accounts> find(String sqlWhere, Object... args) throws Exception {
		logger.debug(sqlWhere);
		logger.debug(Arrays.asList(args).toString());
		return this.baseDao.find(sqlWhere, args);
	}

	public Accounts findById(Object id) throws Exception {
		return this.baseDao.findById(id);
	}

	public int insert(Accounts t) throws Exception {
		return this.baseDao.insert(t);
	}

	public int update(Accounts t) throws Exception {
		return this.baseDao.update(t);
	}

	public int getNextSequence() throws Exception {
		return this.baseDao.getSeqNextValue();
	}
}
